define(function (require, exports, module) {
    var $ = require("jquery");
    var template = require("template");
    
    var dialogModule = require('js/goToLogin');
    var api_prefix= api_domain +"/api/index.php?r=cms/";

    var Nav = {
        initMenus : function (){
            var self = this;
            this.getMenus()
                .then(function(res) {
                    if(!res){
                        dialogModule.dialog.showModal();
                        return;
                    }
                    self.fillMenus(res);                     
                })
        },
        getQueryParams: function (para) {
            var reg = new RegExp("(^|&)" + para + "=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) {
                return decodeURIComponent(r[2]);
            }
            return null;
        },
        getMenus:function () {
            var defer = $.Deferred();
            var url = api_prefix + "cms-data/get-menu&jsonp=" + jsopName;
            $.getScript(url, function () {
                defer.resolve(window[jsopName])
            })
            return defer.promise();
        },
        fillMenus: function(menus){       
            var dirId = this.getQueryParams('dirId');
            var pid = this.getQueryParams('pid');
            if (pid && pid != 0) {
                dirId = pid;
            }
            var html = template('navs', {menus:menus,current:dirId});
            $("#navbar").html(html);
        }
    };
    
    Nav.initMenus();
});